package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.Contracts;
import com.grj.park.service.ContractsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("contracts")
public class ContractsController {

    private final ContractsService contractsService;

    @GetMapping
    public R<List<Contracts>> getInvestors(){
        return R.ok(contractsService.list());
    }

    @PostMapping
    public R<?> saveContracts(@RequestBody Contracts contracts){
        LambdaQueryWrapper<Contracts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contracts::getContractName, contracts.getContractName());
        Contracts one = contractsService.getOne(queryWrapper);
        if (one != null){
            return R.fail("合约名字已存在");
        }
        contractsService.save(contracts);
        return R.okMsg("添加合约成功");
    }
}
